package com.szq.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.szq.yygh.client.cmn.DictFeginClient;
import com.szq.yygh.hosp.model.hosp.Hospital;
import com.szq.yygh.hosp.repository.HospitalRepository;
import com.szq.yygh.hosp.service.HospitalService;
import com.szq.yygh.hosp.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/***
 * All rights Reserved, Designed By www.jx-lab.com
 * @Title: HospitalServiceImpl
 * @Description:
 * @author lixy
 * @date 2021/8/19 15:18
 * @version V1.0
 * @Copyright: www.jx-lab.com Inc. All rights reserved.
 * 注意：本内容仅限于众合景轩信息技术有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeginClient dictFeginClient;


    @Override
    public void saveHospital(Map<String, Object> requestMap) {
        String jsonStr = JSONObject.toJSONString(requestMap);
        Hospital hospital = JSONObject.parseObject(jsonStr, Hospital.class);
        //判断是否存在
        Hospital oldHospital = hospitalRepository.findHospitalByHoscode(hospital.getHoscode());
        if (null != oldHospital){
            hospital.setStatus(oldHospital.getStatus());
            hospital.setCreateTime(oldHospital.getCreateTime());
        }else{
            hospital.setStatus(1);
            hospital.setCreateTime(new Date());
        }
        hospital.setUpdateTime(new Date());
        hospital.setIsDeleted(0);
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital findByHoscode(String hoscode) {
        return hospitalRepository.findHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> listHosp(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable = PageRequest.of(page-1, limit);
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        Example<Hospital> example = Example.of(hospital, exampleMatcher);
        Page<Hospital> pageResult = hospitalRepository.findAll(example, pageable);
        pageResult.getContent().stream().forEach(t->{
            this.getDictName(t);
        });
        return pageResult;
    }

    @Override
    public void updateHospStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> getHospitalById(String id) {
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = hospitalRepository.findById(id).get();
        result.put("hospital", this.getDictName(hospital));
        result.put("bookingRule", hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }

    @Override
    public String getHosName(String hoscode) {
        Hospital hospital = hospitalRepository.findHospitalByHoscode(hoscode);
        if (null != hospital){
            return hospital.getHosname();
        }
        return null;
    }

    private Hospital getDictName(Hospital t) {
        Map<String, Object> param = t.getParam();
        param.put("hostypeString", dictFeginClient.getName("Hostype", t.getHostype()));
        param.put("fullAddress", dictFeginClient.getName(t.getProvinceCode()) +
                dictFeginClient.getName(t.getCityCode()) + dictFeginClient.getName(t.getDistrictCode()) );
        t.setParam(param);
        return t;
    }


}
